<template>
  <div class="app-container myComment">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章" prop="articleId">
        <el-select v-model="queryParams.articleId" placeholder="请选择文章" clearable>
          <el-option
            v-for="article in articleOptionsAll"
            :key="article.articleId"
            :label="article.title"
            :value="article.articleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评论" prop="commentContext">
        <el-input
          v-model="queryParams.commentContext"
          placeholder="请输入评论"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否置顶" prop="topping">
        <el-select v-model="queryParams.topping" placeholder="请选择是否置顶" clearable>
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择是否显示" clearable>
          <el-option
            v-for="dict in sys_show_hide"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评论id" prop="commentId">
        <el-input
          v-model="queryParams.commentId"
          placeholder="请输入博客评论id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['blog:comment:add']"
        >评论</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="commentList"
      row-key="commentId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="评论id" prop="commentId" width="100"/>
      <el-table-column label="所属" prop="articleName" width="200"/>
      <el-table-column label="评论" align="center" prop="commentContext" >
        <template #default="scope">
          <v-md-preview  :text="scope.row.commentContext"></v-md-preview>
        </template>
      </el-table-column>
      <el-table-column label="置顶" align="center" prop="topping" width="100">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.topping"/>
        </template>
      </el-table-column>
      <el-table-column label="显示" align="center" prop="isShow" width="100">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.isShow"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="100"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['blog:comment:edit']">修改</el-button>
              <el-button link type="primary" icon="Plus" @click="handleReply(scope.row)" v-hasPermi="['blog:comment:add']">回复</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['blog:comment:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改博客评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="commentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评论" prop="parentId" v-if="form.parentId != 0">
          <v-md-preview :text="form.parentCommentContext"></v-md-preview>
        </el-form-item>
        <el-form-item label="文章" prop="articleId">
            <el-tree-select
              v-model="form.articleId"
              :data="articleOptions"
              :props="{ value: 'articleId', label: 'title', children: 'children' }"
              value-key="articleId"
              placeholder="请选择文章"
              check-strictly
            />
        </el-form-item>
        <el-form-item label="正文" prop="commentContext">
          <el-input v-model="form.commentContext"
            type="textarea"
            placeholder="来条评论吧，支持markdown语法！" />
        </el-form-item>
        <el-form-item label="置顶" prop="topping">
          <el-radio-group v-model="form.topping">
            <el-radio-button
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示" prop="isShow">
          <el-radio-group v-model="form.isShow">
            <el-radio-button
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Comment">
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/blog/comment";
import { selectArticle } from "@/api/blog/article";

const { proxy } = getCurrentInstance();
const { sys_yes_no, sys_show_hide } = proxy.useDict('sys_yes_no', 'sys_show_hide');

const commentList = ref([]);
const commentOptions = ref([]);
const articleOptions = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(false);
const refreshTable = ref(true);

const articleOptionsAll = computed(() => {
  if (articleOptions.value.length == 0){
    return [{ articleId: '0', title: '所有'}]
  }
  return [{ articleId: '0', title: '所有'}, ...articleOptions.value[0].children]
})

const data = reactive({
  form: {},
  queryParams: {
    articleId: undefined,
    commentContext: undefined,
    topping: undefined,
    isShow: undefined,
    commentId: undefined,
  },
  rules: {
    articleId: [
      { required: true, message: "博客文章id不能为空", trigger: "change" }
    ],
    commentContext: [
      { required: true, message: "评论正文不能为空", trigger: "blur" }
    ],
    topping: [
      { required: true, message: "是否置顶不能为空", trigger: "change" }
    ],
    isShow: [
      { required: true, message: "是否显示不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询博客评论列表 */
function getList() {
  loading.value = true;
  listComment(queryParams.value).then(response => {
    commentList.value = proxy.handleTree(response.data, "commentId", "parentId");
    loading.value = false;
  });
}

/** 查询博客评论下拉树结构 */
function getTreeselect() {
  listComment().then(response => {
    commentOptions.value = [];
    const data = { commentId: 0, commentContext: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "commentId", "parentId");
    commentOptions.value.push(data);
  });
}
/** 查询博客评论下拉树结构 */
function getTreeselectArticle() {
  selectArticle().then(response => {
    articleOptions.value = [];
    const data = { articleId: '0', title: '所有', children: []};
    data.children = response.data
    articleOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    commentId: null,
    articleId: '0',
    commentContext: null,
    topping: 0,
    isShow: 0,
    parentId: 0,
    createBy: null,
    createTime: null,
  };
  proxy.resetForm("commentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  if (row != null && row.commentId) {
    form.value.parentId = row.commentId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加评论";
}

/** 回复按钮操作 */
function handleReply(row) {
  reset();
  if (row != null && row.commentId) {
    form.value.parentId = row.commentId;
    form.value.articleId = row.articleId;
    form.value.parentCommentContext = row.commentContext;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "回复评论";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  loading.value = true;
  reset();
  if (row != null) {
    form.value.parentId = row.commentId;
  }
  getComment(row.commentId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改评论";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["commentRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.commentId != null) {
        updateComment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addComment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除博客评论编号为"' + row.commentId + '"的数据项？').then(function() {
    loading.value = true;
    return delComment(row.commentId);
  }).then(() => {
    loading.value = false;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

getList();
getTreeselectArticle()

onActivated(() => {
  getTreeselectArticle()
})
</script>

<style>
.myComment .vuepress-markdown-body:not(.custom){
  padding: 5px !important;
}
</style>
